package com.codingvip.array;

import java.awt.image.Kernel;
import java.util.HashMap;
import java.util.Map;

public class Sum {

    public static int[] result(int[] nums, int target) {
        Map<Integer, Integer> tempMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            tempMap.put(nums[i], i);
        }
        for (int j = 0; j < nums.length; j++) {
            Integer subNumbers = target - nums[j];
            if (tempMap.containsKey(subNumbers) && tempMap.get(subNumbers) != j) {
                return new int[]{tempMap.get(subNumbers), j};

            }
        }

        return new int[]{-1};

    }

    public static int threeSumClosest(int[] nums, int target) {
        Integer closevalue = null;
        Integer tempCloseValue = null;
//        for (int i = 0; i < nums.length; i++) {
//            tempMap.put(nums[i], i);
//        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if (i == j || j == k || i == k) {
                        continue;
                    }
                    tempCloseValue = (nums[j] + nums[i] + nums[k]);
                    if (closevalue == null) {
                        closevalue = tempCloseValue;
                    } else if (Math.abs(target - tempCloseValue) < Math.abs(target - closevalue)) {
                        closevalue = tempCloseValue;
                    }
                }


            }
        }
        if (closevalue != null) {
            return closevalue;
        }
        throw new RuntimeException("异常");
    }

    public static void main(String[] args) {
//        int[] nums = new int[]{2, 6, 7, 9};
//        int[] result = result(nums, 13);
//        for (int i : result) {
//            System.out.print(i + ",");
//        }
        int[] nums = new int[]{-1,2,1,-4};
        System.out.print(threeSumClosest(nums,1));
    }
}
